// pages/bankCardList/bankCardList.js
var common = require("../../utils/Conmon.js");

var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Type: 0,
    Soure: 0,   //来源：1：提现页面  2：充值页面  0:无
    arrayObj: ['支付宝账户', '银行卡账户'],
    bankList: []
  },

  /**
   * 绑定选择事件
   */
  bindPickerChange: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var url = '/pages/withdrawal/withdrawal?bankId=0&type=' + that.data.Type + "&soure=" + that.data.Soure;
    if (e.detail.value > 0) {
      //跳转银联新增
      url = url + '&Index=1';
    } else {
      //跳转到支付宝新增
      url = url + '&Index=0';
    }
    wx.redirectTo({
      url: url,
    })
  },

  /**
   * 绑定删除事件
   */
  bindDeleteCardTap: function (e) {
    var that = this;
    var bankId = e.currentTarget.dataset.bankid;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗?',
      success: function (result) {
        if (result.confirm) {
          wx.showLoading({ title: '删除中...', mask: true });
          wx.request({
            url: appData.apiurl + 'member/api/UserBankInfo/Delete',
            method: 'GET',
            data: { custId: appData.custID, bankId: bankId },
            header: {
              "content-type": "application/json",
              'X_ManuName': appData.manuName
            },
            success: function (res) {
              wx.hideLoading();
              var list = [];
              if (res.statusCode == 200 && res.data.code == 1200) {
                for (var i = 0; i < that.data.bankList.length; i++) {
                  if (i != index)
                    list.push(that.data.bankList[i]);
                }
                that.setData({
                  bankList: list
                })
              }
            },
            fail: function (res) {
              wx.hideLoading()
              wx.showModal({ title: '错误', content: '删除失败', showCancel: false })
              console.log(res.data)
            }
          })
        }
      }
    })
  },

  /**
   * 绑定编辑事件
   */
  bindEditCardTap: function (e) {
    var that = this;
    var sort = e.currentTarget.dataset.sort;
    var bankId = e.currentTarget.dataset.bankid;
    var url = '/pages/withdrawal/withdrawal?bankId=' + bankId + '&type=' + that.data.Type + '&soure=' + that.data.Soure;
    if (sort == '1') {
      url = url + '&Index=0';
    } else {
      url = url + '&Index=1';
    }
    wx.redirectTo({
      url: url,
    })
  },

  /**
   * 绑定选择事件(选中某账户)
   */
  bindClickSelectTap: function (e) {
    var that = this;
    var sort = e.currentTarget.dataset.sort;
    var bankId = e.currentTarget.dataset.bankid;
    var url = '/pages/withdrawal/withdrawal?bankId=' + bankId + '&type=' + that.data.Type + '&soure=' + that.data.Soure;
    if (sort == '1') {
      url = url + '&Index=0';
    } else {
      url = url + '&Index=1';
    }
    //soure :1 为提现  2 为充值  (其他)
    switch (that.data.Soure) {
      case "1":
        url = '/pages/withdrawCash/withdrawCash?bankId=' + bankId + '&type=' + that.data.Type;
        break;
      case "2":
        url = '/pages/topup/topup?bankId=' + bankId + '&type=' + that.data.Type;
        break;
      default:
        break;
    }
    wx.redirectTo({
      url: url,
    })
  },

  /**
   * 绑定初始化加载数据
   */
  bindLoadData: function (_this) {
    wx.request({
      url: appData.apiurl + 'member/api/UserBankInfo/GetBankInfo',
      method: 'GET',
      data: { "custId": appData.custID, "userId": wx.getStorageSync('UserInfo').UserID, type: _this.data.Type },
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        wx.hideLoading()
        if (res.statusCode == 200 && res.data.code == 1200) {
          var data = res.data.content;
          if (data != null) {
            for (var i = 0; i < data.length; i++) {
              var bankNo = data[i].BankNoStr;
              if (bankNo != null && bankNo != undefined) {
                data[i].BankNoStr = bankNo.replace(/(\d{8})\d{4}(\d{4})/, '$1****$2');
              }
            }
            _this.setData({
              bankList: data
            })
          }
        }
      },
      fail: function (res) {
        wx.hideLoading()
        wx.showModal({ title: '错误', content: '请求获取异常错误', showCancel: false })
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () {
    });
    var that = this;
    var soure = options.soure; //来源 1:来源提现界面 2:来源充值界面
    var _type = options.type; //类型 银行卡还是支付宝
    if (_type != null || _type != undefined) {
      that.setData({
        Type: _type,
        Soure: soure
      })
    }
    wx.showLoading({ title: '加载中...', mask: true });
    that.bindLoadData(that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})